package com.chuangjiangx.member.manager.web.web.basic.controller;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlineResponse;
import com.chuangjiangx.member.basic.ddd.application.MbrPayGiftCardRuleApplication;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import com.chuangjiangx.member.basic.ddd.query.MbrPayGiftCardRuleQuery;
import com.chuangjiangx.member.basic.ddd.query.condition.QueryPayGiftCardRuleConfition;
import com.chuangjiangx.member.basic.ddd.query.dto.MbrPayGiftCardRuleDTO;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.basic.request.CreatePayGiftCardRuleRequest;
import com.chuangjiangx.member.manager.web.web.basic.request.ModifyPayGiftCardRuleRequest;
import com.chuangjiangx.member.manager.web.web.basic.response.MbrPayGiftCardRuleResponse;
import com.chuangjiangx.member.score.ddd.application.command.CreatePayGiftCardRuleCommand;
import com.chuangjiangx.member.score.ddd.application.command.ModifyPayGiftCardRuleCommand;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * 支付即会员规则
 *
 * @author wp
 * @date 2018-08-28 13:53
 **/
@Slf4j
@Api(value = "MbrPayGiftCardRuleController", tags = "支付即会员规则", description = "MbrPayGiftCardRuleController")
@RestController
@RequestMapping(value = "/api/mbr/pay-gift-card-rule", produces = "application/json")
public class MbrPayGiftCardRuleController extends BaseController {
    @Autowired
    private MbrPayGiftCardRuleQuery mbrPayGiftCardRuleQuery;
    @Autowired
    private MbrPayGiftCardRuleApplication mbrPayGiftCardRuleApplication;

    /**
     * 添加规则
     *
     * @param session
     * @param createPayGiftCardRuleRequest 创建支付及会员规则请求参数对象
     * @return Response
     * @throws Exception
     */
    @PostMapping(value = "/create")
    @ApiOperation(value = "创建支付即会员规则")
    @Login
    public Response createPayGiftCardRule(@ApiIgnore HttpSession session, @Validated @RequestBody CreatePayGiftCardRuleRequest createPayGiftCardRuleRequest) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Assert.notNull(user, "当前登陆用户不存在");
        Long merchantId = user.getMerchantId();
        CreatePayGiftCardRuleCommand command = new CreatePayGiftCardRuleCommand();
        command.setBeginTime(createPayGiftCardRuleRequest.getBeginTime());
        command.setEndTime(createPayGiftCardRuleRequest.getEndTime());
        command.setGetChannelList(createPayGiftCardRuleRequest.getGetChannelList());
        command.setLeastCost(createPayGiftCardRuleRequest.getLeastCost());
//        command.setMaxCost(createPayGiftCardRuleRequest.getMaxCost());
        command.setMerchantId(new MerchantId(merchantId));
        mbrPayGiftCardRuleApplication.createPayGiftCardRule(command);
        return ResponseUtils.success();
    }

    /**
     * 查询规则
     *
     * @param session
     * @return Response
     * @throws Exception
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "查询支付即会员规则")
    @Login
    public UnderlineResponse<MbrPayGiftCardRuleResponse> queryPayGiftCardRule(@ApiIgnore HttpSession session) throws Exception {

        QueryPayGiftCardRuleConfition queryPayGiftCardRuleConfition = new QueryPayGiftCardRuleConfition();
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Assert.notNull(user, "当前登陆用户不存在");
        Long merchantId = user.getMerchantId();
        Assert.notNull(merchantId, "商户id不存在");
        queryPayGiftCardRuleConfition.setMerchantId(new MerchantId(merchantId));
        MbrPayGiftCardRuleDTO mbrPayGiftCardRuleDTO = mbrPayGiftCardRuleQuery.searchRule(queryPayGiftCardRuleConfition);
        MbrPayGiftCardRuleResponse mbrPayGiftCardRuleResponse = new MbrPayGiftCardRuleResponse();
        CJBeanUtils.convertBean(mbrPayGiftCardRuleDTO, mbrPayGiftCardRuleResponse);
        if (mbrPayGiftCardRuleDTO != null && mbrPayGiftCardRuleDTO.getBeginTime() != null) {
            mbrPayGiftCardRuleResponse.setBeginTime(mbrPayGiftCardRuleDTO.getBeginTime().getTime());
        }
        if (mbrPayGiftCardRuleDTO.getEndTime() != null) {
            mbrPayGiftCardRuleResponse.setEndTime(mbrPayGiftCardRuleDTO.getEndTime().getTime());
        }
        return (UnderlineResponse<MbrPayGiftCardRuleResponse>) ResponseUtils.success(mbrPayGiftCardRuleResponse);
    }

    /**
     * 修改规则
     *
     * @param session
     * @param modifyPayGiftCardRuleRequest 修改支付及会员规则请求参数对象
     * @return Response
     * @throws Exception
     */
    @PutMapping(value = "/updata")
    @ApiOperation(value = "修改支付即会员规则")
    @Login
    public Response updataPayGiftCardRule(@ApiIgnore HttpSession session, @Validated @RequestBody ModifyPayGiftCardRuleRequest modifyPayGiftCardRuleRequest) throws Exception {
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        Assert.notNull(user, "当前登陆用户不存在");
        Long merchantId = user.getMerchantId();
        ModifyPayGiftCardRuleCommand command = new ModifyPayGiftCardRuleCommand();
        command.setRuleId(modifyPayGiftCardRuleRequest.getId());
        command.setBeginTime(modifyPayGiftCardRuleRequest.getBeginTime());
        command.setEndTime(modifyPayGiftCardRuleRequest.getEndTime());
        command.setGetChannelList(modifyPayGiftCardRuleRequest.getGetChannelList());
        command.setLeastCost(modifyPayGiftCardRuleRequest.getLeastCost());
        command.setMerchantId(new MerchantId(merchantId));
        mbrPayGiftCardRuleApplication.updatePayGiftCardRule(command);
        return ResponseUtils.success();
    }

    /**
     * 判断能否开启微信推送
     *
     * @param session
     * @return
     */
//    @PostMapping(value = "/can-open-wx-push")
//    @ApiOperation(value = "判断能否开启微信推送")
//    @Login
//    public Response canOpenWxPush(@ApiIgnore HttpSession session) {
//        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
//        Assert.notNull(user, "当前登陆用户不存在");
//        Long merchantId = user.getMerchantId();
//        CanOpenWxPushCondition canOpenWxPushCondition = new CanOpenWxPushCondition();
//        canOpenWxPushCondition.setMerchantId(new MerchantId(merchantId));
//        boolean canOpenWxPush = mbrPayGiftCardRuleQuery.canOpenWxPush(canOpenWxPushCondition);
//        return ResponseUtils.success(canOpenWxPush);
//    }
}
